package com.li.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.constant.BaseContext;
import com.li.pojo.dto.ContractDto;
import com.li.pojo.dto.ContractPageDto;
import com.li.pojo.po.Contract;
import com.li.mapper.ContractMapper;
import com.li.pojo.po.Opportunities;
import com.li.pojo.po.PageResult;
import com.li.service.BusinessService;
import com.li.service.ContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 合同 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-14
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {

    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private BusinessService businessService;

    @Override
    public void addContract(ContractDto contractDto) {
        Contract contract = new Contract();
        BeanUtils.copyProperties(contractDto,contract);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        contract.setUpdateTime(LocalDateTime.now().format(formatter));
        contract.setUpdateBy(BaseContext.getCurrentName());
        contract.setCreateBy(BaseContext.getCurrentName());
        contract.setCreateTime(LocalDateTime.now().format(formatter));
        contractMapper.insert(contract);
    }

    @Override
    public PageResult findByPage(ContractPageDto pageDto) {
        Page<Contract> page = PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
        List<Contract> list =contractMapper.findByPage(pageDto);
        return new PageResult(200,"分页查询成功",null,page.getResult(),page.getTotal());
    }

    @Override
    public Contract findById(Integer id) {
        Contract contract = contractMapper.findById(id);
        return contract;
    }

    @Override
    public void updateContract(ContractDto contractDto) {
        Contract contract = new Contract();
        BeanUtils.copyProperties(contractDto,contract);
        contract.setUpdateBy(BaseContext.getCurrentName());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        contract.setUpdateTime(LocalDateTime.now().format(formatter));
        contractMapper.updateById(contract);
    }

    @Override
    public void changeContract(Integer id, ContractDto contractDto) {
        Opportunities opportunities = businessService.find(id);
        Contract contract = new Contract();
        BeanUtils.copyProperties(contractDto,contract);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        contract.setUpdateTime(LocalDateTime.now().format(formatter));
        contract.setCreateTime(LocalDateTime.now().format(formatter));
        contract.setName(opportunities.getName());
        contract.setSubject(opportunities.getSubject());
        contract.setChannel(opportunities.getChannel());
        contract.setStatus("4");
        contract.setActivityId(opportunities.getActivityId());
        contract.setCreateBy(BaseContext.getCurrentName());
        contract.setUpdateBy(BaseContext.getCurrentName());
//        contract.setCourseId(Long.parseLong(opportunities.getCourseId()));
        opportunities.setBusinessId(opportunities.getId().toString());
        opportunities.setStatus("7");
        businessService.update(opportunities);
        contractMapper.insert(contract);
    }
}
